package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
//import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {

    @Override
    public List<CourseBase> queryAll() {
        List<CourseBase> list = this.list();
        return list;
    }

    @Override
    public PageVO queryCourseBaseList(PageRequestParams params, QueryCourseBaseModel model, Long companyId) {
//            1.判断是否需要开启事务(查询,所以不需要开启事务)
//            2.判断关键数据(分页数据,条件查询数据)
        //判断分页数据
        Long pageNo = params.getPageNo();
        Integer pageSize = params.getPageSize();
        if (ObjectUtils.isEmpty(pageNo)||pageNo<0){
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }
        if (ObjectUtils.isEmpty(pageSize)||pageSize<0){
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }
//            3.构建mp分页对象
        Page<CourseBase> courseBasePage = new Page<>(pageNo,pageSize);
//            4.构建条件查询对象
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtil.isNotBlank(model.getCourseName())){

            queryWrapper.like(CourseBase::getName,model.getCourseName());
        }
        if (StringUtil.isNotBlank(model.getAuditStatus())){
            queryWrapper.eq(CourseBase::getAuditStatus,model.getAuditStatus());
        }
        queryWrapper.eq(CourseBase::getCompanyId,companyId);
        queryWrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG);
//            5.查询数据
        Page<CourseBase> page = this.page(courseBasePage, queryWrapper);
        List<CourseBase> records = page.getRecords();
        long total = page.getTotal();
        //将po数据装weidto数据
        //TODO:
        List<CourseBaseDTO> dtos = Collections.EMPTY_LIST;
        if (!CollectionUtils.isEmpty(records)){
            dtos= CourseBaseConvert.INSTANCE.entitys2Dtos(records);
        }

//            6.封装数据,进行返回
        PageVO pageVO = new PageVO(dtos,total,pageNo,pageSize);
        return pageVO;
    }
}
